package com.zgroup.services

import com.zgroup.common.CheckUtils
import com.zgroup.domain.Dictionary
import com.zgroup.domain.Estate

class DictionaryService {

  boolean transactional = true

  def loadDic(String dicName) {
    return loadDic(dicName, null)
  }

  def loadDic(String dicName, String parent) {
    def params = [dicName]
    String sql = "select new map(code as DM,name as MC,pinyin as PY) from com.zgroup.domain.Dictionary where type=? "
    if (!CheckUtils.isEmpty(parent)) {
      sql += " and code like ?"
      params.add(parent + "%")
    }
    sql += " order by sortNo"
    Dictionary.executeQuery(sql, params)
  }

  def loadDomain(String domainName, String sortKey) {
    String sql = "select new map(id as DM,name as MC,name as PY) from com.zgroup.domain." + domainName + " where showFlag='1' order by " + sortKey
    Dictionary.executeQuery(sql)
  }

  def loadEstate(params) {
    String sql = "select new map(id as DM,name as MC,pinyin as PY) from com.zgroup.domain.Estate where showFlag='1'"
    List conditions = []
    def item = params.sector
    if (!CheckUtils.isEmpty(item)) {
      sql += " and sector = ?"
      conditions.add(item)
    }
    item = params.estateType
    if (!CheckUtils.isEmpty(item)) {
      sql += " and type like ?"
      conditions.add(item + "%")
    }
    sql += " order by name"
    Estate.executeQuery(sql, conditions)
  }
}
